#include <stdio.h>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <iostream>

std::vector<std::string> &split(const std::string &s, char delim,
								std::vector<std::string> &elems) {
	std::stringstream ss(s);
	std::string item;
	while(std::getline(ss, item, delim)) {
		elems.push_back(item);
	}
	return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
	std::vector<std::string> elems;
	return split(s, delim, elems);
}

void copy(std::string sIn, std::string sOut, int iLineCnt)
{
	std::ifstream fIn;
	std::ofstream fOut;
	int iCnt;
	std::string sLine;

	fIn.open(sIn.c_str());
	if (!fIn.is_open())
	{
		std::cout << "Invalid input file!";
		return;
	}
	fOut.open(sOut.c_str());
	if (!fOut.is_open())
	{
		std::cout << "Invalid output file!";
		fIn.close();
		return;
	}

	for (iCnt = 0; !fIn.eof() && iCnt <iLineCnt; ++iCnt)
	{
		std::getline(fIn, sLine);
		if (sLine.size() > 0)
			fOut << sLine << std::endl;
	}
	fIn.close();
	fOut.flush();
	fOut.close();
}

void main()
{
// 
// 	copy("D:\\Users\\phvu\\Documents\\Projects\\Adsorption\\data\\MovieLens\\100k_u.data",
// 		"D:\\Users\\phvu\\Documents\\Projects\\Adsorption\\data\\MovieLens\\100k_u_10k.data", 10000);
// 	return;

	std::string sInput, sOutput, sLine;
	std::ifstream fIn;
	std::ofstream fOut;
	std::vector<std::string> vtLine;

	sInput = "G:\\MovieLens\\ml-data_0\\u5.base";
	sOutput = "D:\\Users\\phvu\\Documents\\Projects\\Adsorption\\data\\MovieLens\\100k_u5.base";

	fIn.open(sInput.c_str());
	if (!fIn.is_open())
	{
		std::cout << "Invalid input file!";
		return;
	}
	fOut.open(sOutput.c_str());
	if (!fOut.is_open())
	{
		std::cout << "Invalid output file!";
		fIn.close();
		return;
	}

	while (!fIn.eof())
	{
		std::getline(fIn, sLine);
		vtLine.clear();
		split(sLine, '\t', vtLine);

		if (vtLine.size() >= 3)
		{
			fOut << vtLine[0] << "\t" << 
					vtLine[1] << "\t" << 
					vtLine[2] << std::endl;
		}		
	}
	fIn.close();
	fOut.flush();
	fOut.close();
}